package com.csiec.clock.view.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.lifecycle.ViewModel;

import com.csiec.clock.R;
import com.csiec.clock.activity.FaceActivity;
import com.csiec.clock.activity.UserInfoActivity;
import com.csiec.clock.activity.daka.JoinClockInfoActivity;
import com.csiec.clock.bean.UserInfo;
import com.csiec.clock.bean.face.ResultCode;
import com.csiec.clock.bean.myJoinClock.InformationList;
import com.csiec.clock.bean.myJoinClock.JoinClock;
import com.csiec.clock.callback.FaceCallBack;
import com.csiec.clock.databinding.ActivityFaceBinding;
import com.csiec.clock.model.FaceModel;
import com.csiec.clock.utils.Base64Util;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;


public class FaceViewModel extends ViewModel {
    @SuppressLint("StaticFieldLeak")
    private Activity activity;
    private ActivityFaceBinding binding;
    private String activityInfo;
    private JoinClock ownClock;

    private final int[] bgList = new int[]{R.drawable.huang, R.drawable.bai, R.drawable.lv, R.drawable.hong};

    private FaceModel model;
    private final android.os.Handler handler = new Handler();

    public void init(Activity activity, ActivityFaceBinding binding, String activityInfo, JoinClock ownClock) {
        model = new FaceModel();
        this.activity = activity;
        this.binding = binding;
        this.activityInfo = activityInfo;
        this.ownClock = ownClock;
        startMethod();

    }

    private <T> void changActivity(String msg, Class<T> tClass) {
        Intent intent = new Intent(activity, tClass);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("faceInfo", msg);
        intent.putExtra("join_card", ownClock);
        activity.startActivity(intent);
        activity.finish();
    }

    //活体检测
    private void remakeDetection() {
        model.remakeDetection(new FaceCallBack() {
            @Override
            public void onSuccess(ResultCode backData) {
                if (backData.getStatus() == 200) {
                    handler.post(() -> {
                        switch (Integer.parseInt(activityInfo)) {
                            case 0:
                                //信息绑定
                                changActivity("验证成功", UserInfoActivity.class);
                                break;
                            case 1:
                                //打卡
                                changActivity("验证成功", JoinClockInfoActivity.class);
                                break;
                        }
                    });
                } else {
                    handler.post(() -> {
                        switch (Integer.parseInt(activityInfo)) {
                            case 0:
                                //信息绑定
                                changActivity("验证失败", UserInfoActivity.class);
                                break;
                            case 1:
                                //打卡
                                changActivity("验证失败", JoinClockInfoActivity.class);
                                break;
                        }
                    });
                }
            }

            @Override
            public void onFailed() {
                handler.post(() -> {
                    showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络异常");
                });
            }
        });
    }

    final Camera.PictureCallback jpeg = (data, camera) -> {
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        Bitmap compressBitmap = Base64Util.compressBitmap(bitmap, 300);

        String s = Base64Util.bitmap2Base64(compressBitmap);
        UserInfo.getUserInfo().setBase64(s);
        remakeDetection();
    };

    public void getPic() {
        binding.circleCameraPreview.setOnPreview((nv21, camera) -> {
            // 人脸识别代码
            if (camera != null) {
                camera.takePicture(null, null, jpeg);
            }
        });
    }

    /**
     * 通过Handler延迟发送消息的形式实现定时任务。
     */
    public static final int CHANGE_TIPS_TIMER_INTERVAL = 1000;
    private final Handler mChangeTipsHandler = new Handler(Looper.getMainLooper());
    int i = 0;

    private void startMethod() {
        Runnable mChangeTipsRunnable = new Runnable() {
            @Override
            public void run() {
                if (i < 4) {
                    try {
                        binding.constraintlayout10.setBackgroundResource(bgList[i++]);
                        mChangeTipsHandler.postDelayed(this, CHANGE_TIPS_TIMER_INTERVAL);
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else if (i == 4) {
                    getPic();
                }
            }
        };
        mChangeTipsHandler.post(mChangeTipsRunnable);
    }

    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.textView26.postDelayed(tipDialog::dismiss, 2000);
    }
}
